﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace VoiceAd.Common
{
    public class MyFunction
    {
        public class HttpParamActionAttribute : ActionNameSelectorAttribute
        {
            public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
            {
                if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
                    return true;

                if (!actionName.Equals("Action", StringComparison.InvariantCultureIgnoreCase))
                    return false;

                var request = controllerContext.RequestContext.HttpContext.Request;
                return request[methodInfo.Name] != null;
            }
        }

        public static string FormatString(string strNam, string strThang)
        {
            string strResult = string.Empty;
            strResult = strNam + ((int.Parse(strThang) > 9) ? strThang : "0" + strThang);
            return strResult;
        }

        public static string lstPaging(int intSum, int intCurrent, string pAction)
        {
            string strResult = " ";
            int intStart = Math.Max(1, intCurrent - 3);
            int intEnd = Math.Min(intStart + 10, intSum + 1);
            for (int i = intStart; i < intEnd; i++)
            {
                if (i == intCurrent)
                    strResult += "<span class='CurPag'>" + i.ToString() + "</span>|";
                else
                    strResult += "<a href='" + pAction + i.ToString() + ".html'>" + i.ToString() + "</a>|";

            }
            return strResult.Substring(0, strResult.Length - 1);
        }

        public static bool ValidateNumber(string number)
        {
            var regex = new System.Text.RegularExpressions.Regex(@"(^[0-9]{0,}$)");
            if (regex.IsMatch(number))
            {
                return true;
            }
            return false;
        }
    }
}